// ActionScript file
package Avocado.util {

	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.external.ExternalInterface;

	public class MoveObject {
		
		private var target:DisplayObject;

		public function MoveObject(target:DisplayObject, stopped:Boolean):void {
			if (!target.stage) {
				target.addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void {
					if (stopped == true) {
						if (target.hasOwnProperty('currentFrame')) {
							target.gotoAndStop(1);
						}
					}
					target.stage.addEventListener(KeyboardEvent.KEY_DOWN, movementListener);
				});
			} else {
				if (stopped == true) {
					if (target.hasOwnProperty('currentFrame')) {
						target.gotoAndStop(1);
					}
				}
				target.stage.addEventListener(KeyboardEvent.KEY_DOWN, movementListener);
			}
			
			function movementListener(e:KeyboardEvent):void{
				var xx:Number = target.x;
				var yy:Number = target.y;
				var rot:Number = target.rotation;
				var scx:Number = target.scaleX;
				var scy:Number = target.scaleY;
				var frm:Number = 0;
				if (target.hasOwnProperty('currentFrame')) {
					frm = target.currentFrame;
				}
				switch (e.charCode) {
					// position
					case 100:
						xx++;
						break;
					case 97:
						xx--;
						break;
					case 119:
						yy--;
						break;
					case 115:
						yy++;
						break;

					// postion great
					case 87:
						yy = yy - 10;
						break;
					case 83:
						yy = yy + 10;
						break;
					case 68:
						xx = xx + 10;
						break;
					case 65:
						xx = xx - 10;
						break;
					
					//rotation
					case 114:
						scx = scx + .01;
						scy = scy + .01;
						break;
					case 102:
						scx = scx - .01;
						scy = scy - .01;
						break;
					
					//rotation
					case 113:
						rot++;
						break;
					case 101:
						rot--;
						break;
					
					// frame
					case 44:
						if (target.hasOwnProperty('currentFrame')) {
							frm = target.currentFrame -1;
						}
						break;
					case 46:
						if (target.hasOwnProperty('currentFrame')) {
							frm = target.currentFrame +1;
						}
						break;

				}

				if (stopped == true) {
					if (target.hasOwnProperty('currentFrame') && target.hasOwnProperty('gotoAndStop')) {
						target.gotoAndStop(frm);
					}
				}
				target.x = xx;
				target.y = yy;
				target.rotation = rot;
				target.scaleX = scx;
				target.scaleY = scy;

				
				var strToTrace:String = new String();
				strToTrace += '{';
				strToTrace += 'x: ' + xx + ', ';
				strToTrace += 'y: ' + yy + ', ';
				strToTrace += 'rotation: ' + rot + ', ';
				strToTrace += 'scaleX: ' + scx + ', ';
				strToTrace += 'scaleY: ' + scy + ', ';
				strToTrace += 'frame: ' + frm;
				strToTrace += '}';
				
				if (ExternalInterface.available){
					try { ExternalInterface.call('console.debug', strToTrace); } catch (err:Error) { trace(err); }
				}
				trace(strToTrace);
			}
		}
		
		public static function move(target:DisplayObject, stopped:Boolean=false):MoveObject {
			return new MoveObject(target, stopped);
		}
		
	}
}